﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

public partial class System_Pages_Content_FileManager : SiteFox.Base.Page
{
    string _RootPath = "~/Web";

    #region PageEvents
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lab_SelectedPath.Text = _RootPath;
            this.RefreshGridView();
            this.SetGoUpButton();
            //fill hidden object with extensions allowed for upload.
            string _exts = "";
            IList<SiteFox.Business.Model.Extension> _Extensions = SiteFox.Business.Manager.Extension.GetAll();
            for (int i = 0; i < _Extensions.Count; i++)
            {
                SiteFox.Business.Model.Extension _Extension = _Extensions[i];
                if (_Extension.AllowUpload)
                {
                    _exts += _Extension.Ext + ";";
                }
            }
            hf_UploadExtList.Value = _exts;
        }
    }
    #endregion

    #region ControlsEvents
    protected void lib_Name_Click(object sender, EventArgs e)
    {
        LinkButton myLib = (LinkButton)sender;
        string newPath = lab_SelectedPath.Text + "/" + myLib.CommandArgument;
        gv_Files.DataSource = SiteFox.Business.Logic.FileManager.GetAllContent(newPath);
        gv_Files.DataBind();
        lab_SelectedPath.Text = newPath;
        this.SetGoUpButton();
    }

    protected void imb_NewFolder_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_NewFolder.Visible = true;
        up_Content.Update();
    }
    protected void lib_NewFolder_Close_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = false;
        pan_NewFolder.Visible = false;
    }
    protected void lib_NewFolder_Create_Click(object sender, EventArgs e)
    {
        string _Message = "";
        SiteFox.Business.Logic.FileManager.CreateNewFolder(lab_SelectedPath.Text, ucc_NewFolder_Name.Value.ToString(), out _Message);
        pan_NewFolder.Visible = false;
        ucc_NewFolder_Name.Value = "";
        this.RefreshGridView();
        ucc_MessageBox.Visible = true;
        ucc_MessageBox.Text = _Message;
    }

    protected void imb_Copy_Click(object sender, ImageClickEventArgs e)
    {
        if (GetSelectedObjects().Rows.Count > 0)
        {
            lab_CopyOrMove_Title.Text = Resources.dcCommon.dc_Copy;
            lib_CopyOrMove_DoAction.Text = Resources.dcCommon.dc_Copy;
            pan_Background.Visible = true;
            pan_CopyOrMove.Visible = true;
        }
        else
        {
            ucc_MessageBox.Text = Resources.dcCommon.dc_PleaseSelectFilesOrFoldersToContinueWithAction;
            ucc_MessageBox.Visible = true;
        }
        up_Content.Update();
    }
    protected void imb_Move_Click(object sender, ImageClickEventArgs e)
    {
        if (GetSelectedObjects().Rows.Count > 0)
        {
            lab_CopyOrMove_Title.Text = Resources.dcCommon.dc_Move;
            lib_CopyOrMove_DoAction.Text = Resources.dcCommon.dc_Move;
            pan_Background.Visible = true;
            pan_CopyOrMove.Visible = true;
        }
        else
        {
            ucc_MessageBox.Text = Resources.dcCommon.dc_PleaseSelectFilesOrFoldersToContinueWithAction;
            ucc_MessageBox.Visible = true;
        }
        up_Content.Update();
    }
    protected void lib_CopyOrMove_DoAction_Click(object sender, EventArgs e)
    {
        if (lab_SelectedPath.Text == ucc_FileManager_TreeView.SelectedVirtualPath)
        {
            lab_CopyOrMove_MessageText.Text = Resources.dcCommon.dc_SourceFolderAndDestinationFolderAreTheSame;
        }
        else if (ucc_FileManager_TreeView.SelectedVirtualPath == "")
        {
            lab_CopyOrMove_MessageText.Text = Resources.dcCommon.dc_PleaseSelectDestinationFolder;
        }
        else
        {
            double ActionDone = 0;
            DataTable ActionStatus = new DataTable();
            if (lab_CopyOrMove_Title.Text == Resources.dcCommon.dc_Copy)
            {
                SiteFox.Business.Logic.FileManager.CopyOrMove(GetSelectedObjects(), SiteFox.Business.Logic.FileManager.ActionToDo.Copy, lab_SelectedPath.Text, ucc_FileManager_TreeView.SelectedVirtualPath, Convert.ToBoolean(ucc_CopyOrMove_Overwrite.Value), out ActionStatus, out ActionDone);
            }
            else if (lab_CopyOrMove_Title.Text == Resources.dcCommon.dc_Move)
            {
                SiteFox.Business.Logic.FileManager.CopyOrMove(GetSelectedObjects(), SiteFox.Business.Logic.FileManager.ActionToDo.Move, lab_SelectedPath.Text, ucc_FileManager_TreeView.SelectedVirtualPath, Convert.ToBoolean(ucc_CopyOrMove_Overwrite.Value), out ActionStatus, out ActionDone);
            }
            gv_Files.DataSource = SiteFox.Business.Logic.FileManager.GetAllContent(ucc_FileManager_TreeView.SelectedVirtualPath);
            gv_Files.DataBind();
            lab_SelectedPath.Text = ucc_FileManager_TreeView.SelectedVirtualPath;
            SetGoUpButton();

            if (ActionDone == 100)
            {
                lab_CopyOrMove_MessageText.ForeColor = Color.Green;
            }
            else if (ActionDone < 100 && ActionDone > 0)
            {
                lab_CopyOrMove_MessageText.ForeColor = Color.Orange;
            }
            else if (ActionDone == 0)
            {
                lab_CopyOrMove_MessageText.ForeColor = Color.Red;
            }
            lab_CopyOrMove_MessageText.Text = Resources.dcCommon.dc_RequestedActionDone + " : " + ActionDone.ToString("F") + " %";
            lab_CopyOrMove_MessageText.Font.Bold = true;
            lib_CopyOrMove_DoAction.Visible = false;
            lib_CopyOrMove_ShowDetails.Visible = true;
            gw_Result.DataSource = ActionStatus;
            gw_Result.DataBind();
        }
    }
    protected void lib_CopyOrMove_ShowDetails_Click(object sender, EventArgs e)
    {
        pan_CopyOrMove.Visible = false;
        lib_CopyOrMove_DoAction.Visible = true;
        lib_CopyOrMove_ShowDetails.Visible = false;
        lab_CopyOrMove_MessageText.Text = "";

        pan_Result.Visible = true;
    }
    protected void lib_CopyOrMove_Close_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = false;
        pan_CopyOrMove.Visible = false;
        lib_CopyOrMove_DoAction.Visible = true;
        lab_CopyOrMove_MessageText.Text = "";
    }

    protected void imb_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (GetSelectedObjects().Rows.Count > 0)
        {
            pan_Background.Visible = true;
            pan_Delete.Visible = true;
        }
        else
        {
            ucc_MessageBox.Text = Resources.dcCommon.dc_PleaseSelectFilesOrFoldersToContinueWithAction;
            ucc_MessageBox.Visible = true;
        }
        up_Content.Update();
    }
    protected void lib_Delete_Close_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = false;
        pan_Delete.Visible = false;
    }
    protected void lib_Delete_Ok_Click(object sender, EventArgs e)
    {
        SiteFox.Business.Logic.FileManager.Delete(GetSelectedObjects(), lab_SelectedPath.Text);
        pan_Background.Visible = false;
        pan_Delete.Visible = false;
        this.RefreshGridView();
    }

    protected void lib_Result_Close_Click(object sender, EventArgs e)
    {
        pan_Background.Visible = false;
        pan_Result.Visible = false;
    }

    protected void afu_Upload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        if (afu_Upload.HasFile)
        {
            string _FileName = afu_Upload.FileName;
            string _FileExt = _FileName.Substring(_FileName.LastIndexOf(".") + 1);
            SiteFox.Business.Model.Extension _Extension = SiteFox.Business.Manager.Extension.GetByExt(_FileExt);
            if (_Extension != null)
            {
                if (_Extension.AllowUpload)
                {
                    afu_Upload.SaveAs(Server.MapPath(lab_SelectedPath.Text + "/" + afu_Upload.FileName));
                }
            }
        }
    }
    protected void imb_Upload_Close_Click(object sender, ImageClickEventArgs e)
    {
        this.RefreshGridView();
        up_Content.Update();
    }

    protected void imb_NewFile_Click(object sender, ImageClickEventArgs e)
    {
        pan_Background.Visible = true;
        pan_NewFile.Visible = true;
        pan_NewFile_Ext.Visible = true;
        DataList_NewFile.DataSource = SiteFox.Business.Manager.Extension.GetByCreateNew();
        DataList_NewFile.DataBind();
        up_Content.Update();
    }
    protected void lib_NewFile_Close_Click(object sender, EventArgs e)
    {
        this.NewFile_ResetForm();
    }
    protected void DataList_NewFile_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataList_NewFile.SelectedIndex = e.Item.ItemIndex;
        hf_NewFile_Ext.Value = ((Label)DataList_NewFile.SelectedItem.FindControl("lb_Ext")).Text;
        lib_NewFile_Next.Visible = true;
    }
    protected void lib_NewFile_Next_Click(object sender, EventArgs e)
    {
        pan_NewFile_Ext.Visible = false;
        pan_NewFile_Info.Visible = true;
        lib_NewFile_Next.Visible = false;
        lib_NewFile_Create.Visible = true;

        if (hf_NewFile_Ext.Value == "master")
        {
            pan_NewFile_UseMasterPage.Visible = true;
            ucc_NewFile_UseWpZone.Visible = true;
            ucc_NewFile_UseNavigation.Visible = true;
        }
        else if (hf_NewFile_Ext.Value == "aspx")
        {
            ucc_NewFile_PageTitle.Visible = true;
            pan_NewFile_UseMasterPage.Visible = true;
        }
    }
    protected void lib_NewFile_Create_Click(object sender, EventArgs e)
    {
        string _Message = "";
        if (hf_NewFile_Ext.Value == "master")
        {
            SiteFox.Business.Logic.FileManager.CreateNewFormAsMaster(lab_SelectedPath.Text, ucc_NewFile_Name.Value.ToString(), hf_NewFile_Ext.Value, cb_NewFile_UseMasterPage_Value.Checked, ucc_NewFile_UseWpZone.Value, ucc_NewFile_UseNavigation.Visible, ucc_NewFile_MasterPagePath.Value.ToString(), out _Message);
        }
        else if (hf_NewFile_Ext.Value == "aspx")
        {
            SiteFox.Business.Logic.FileManager.CreateNewFormAsPage(lab_SelectedPath.Text, ucc_NewFile_Name.Value.ToString(), hf_NewFile_Ext.Value, ucc_NewFile_PageTitle.Value.ToString(), cb_NewFile_UseMasterPage_Value.Checked, ucc_NewFile_MasterPagePath.Value.ToString(), out _Message);
        }
        else
        {
            SiteFox.Business.Logic.FileManager.CreateNewTextFile(lab_SelectedPath.Text, ucc_NewFile_Name.Value.ToString(), hf_NewFile_Ext.Value, out _Message);
        }
        
        this.NewFile_ResetForm();
        this.RefreshGridView();
        ucc_MessageBox.Visible = true;
        ucc_MessageBox.Text = _Message;
    }
    protected void cb_NewFile_UseMasterPage_Value_CheckedChanged(object sender, EventArgs e)
    {
        ucc_NewFile_MasterPagePath.Visible = cb_NewFile_UseMasterPage_Value.Checked;
        ucc_NewFile_MasterPagePath.IsReq = cb_NewFile_UseMasterPage_Value.Checked;
        if (cb_NewFile_UseMasterPage_Value.Checked == true)
        {
            ucc_NewFile_UseWpZone.Visible = false;
        }
        else
        {
            ucc_NewFile_UseWpZone.Visible = true;
        }
    }
    #endregion

    #region Methods
    void SetGoUpButton()
    {
        if (lab_SelectedPath.Text == _RootPath)
        {
            imb_GoUp.Enabled = false;
        }
        else
        {
            imb_GoUp.Enabled = true;
        }
    }
    protected void imb_GoUp_Click(object sender, ImageClickEventArgs e)
    {
        string newPath = SiteFox.Business.Logic.FileManager.GetParentPath(lab_SelectedPath.Text);
        gv_Files.DataSource = SiteFox.Business.Logic.FileManager.GetAllContent(newPath);
        gv_Files.DataBind();
        lab_SelectedPath.Text = newPath;
        this.SetGoUpButton();
        up_Content.Update();
    }
    DataTable GetSelectedObjects()
    {
        DataTable myTable = new DataTable();
        DataColumn myColumn;
        DataRow myRow;
        //Table definition
        myColumn = new DataColumn();
        myColumn.DataType = Type.GetType("System.String");
        myColumn.ColumnName = "Name";
        myTable.Columns.Add(myColumn);

        myColumn = new DataColumn();
        myColumn.DataType = Type.GetType("System.String");
        myColumn.ColumnName = "Type";
        myTable.Columns.Add(myColumn);

        for (int i = 0; i < gv_Files.Rows.Count; i++)
        {
            GridViewRow gvr = gv_Files.Rows[i];
            CheckBox cb = (CheckBox)gvr.FindControl("cb_SelectedRow");
            if (cb != null && cb.Checked)
            {
                LinkButton lib = (LinkButton)gvr.FindControl("lib_Name");
                Label lab = (Label)gvr.FindControl("lab_Type");
                myRow = myTable.NewRow();
                myRow["Name"] = lib.Text;
                myRow["Type"] = lab.Text;
                myTable.Rows.Add(myRow);
            }
        }
        return myTable;
    }
    void NewFile_ResetForm()
    {
        pan_Background.Visible = false;
        pan_NewFile.Visible = false;

        pan_NewFile_Ext.Visible = false;
        DataList_NewFile.SelectedIndex = -1;
        lib_NewFile_Next.Visible = false;

        pan_NewFile_Info.Visible = false;
        ucc_NewFile_Name.Value = "";
        ucc_NewFile_PageTitle.Value = "";
        ucc_NewFile_PageTitle.Visible = false;
        pan_NewFile_UseMasterPage.Visible = false;
        cb_NewFile_UseMasterPage_Value.Checked = false;
        ucc_NewFile_MasterPagePath.Visible = false;
        ucc_NewFile_MasterPagePath.IsReq = false;
        ucc_NewFile_UseWpZone.Visible = false;
        ucc_NewFile_UseNavigation.Visible = false;

        hf_NewFile_ExtType.Value = "";
        hf_NewFile_Ext.Value = "";
    }
    void RefreshGridView()
    {
        gv_Files.DataSource = SiteFox.Business.Logic.FileManager.GetAllContent(lab_SelectedPath.Text);
        gv_Files.DataBind();
    }
    #endregion
}